package com.fudan.biz.spark.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fudan.biz.spark.entity.SparkJobConfig;
import com.fudan.biz.spark.service.SparkJobConfigService;
import com.fudan.framework.base.DaoService;
import com.fudan.framework.base.DataController;
import com.fudan.framework.entity.TableJson;
import com.fudan.framework.utils.EntityUtils;
import com.fudan.sys.entity.User;
import com.fudan.sys.service.UserService;

@Controller
@RequestMapping("/biz/spark/conf")
public class SparkRunnerController extends DataController<SparkJobConfig>{

	@Autowired
	private SparkJobConfigService sparkJobConfigService;

	@Autowired
	private UserService userService;

	@Override
	protected DaoService<SparkJobConfig> injectService() {
		return sparkJobConfigService;
	}

	@Override
	protected String pathPrefix() {
		return "biz/spark/";
	}


	@RequestMapping("/list")
	@ResponseBody
	public TableJson list(HttpServletRequest request) {
		List<SparkJobConfig> list = sparkJobConfigService.pages(request);
		long count = sparkJobConfigService.countAll();
		TableJson tableJson = TableJson.build(list, count);
		return tableJson;
	}

	@Override
	public Boolean add(HttpServletRequest request) throws Exception {
		SparkJobConfig entity = EntityUtils.toEntity(request, SparkJobConfig.class);
		HttpSession session = request.getSession(false);
		if(session != null) {
			String username = (String) session.getAttribute("username");
			User user = userService.getUserByName(username);
			entity.setUserId(user.getId());
			return sparkJobConfigService.add(entity);
		}
		return false;
	}

}
